package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.musicroomrank.UserMusicRoomRankBeautyData;
import com.yanqu.road.entity.activity.musicroomrank.UserMusicRoomRankData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMusicRoomRankDaoImpl extends TempDataObjectDao<UserMusicRoomRankData> {
    public static String getUserMusicRoomRankDataSql = "select * from `t_u_activity_music_room_rank` where `activityId`=? and `userId`=?";

    public UserMusicRoomRankData getUserMusicRoomRankData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMusicRoomRankDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserMusicRoomRankDataSql = "update `t_u_activity_music_room_rank` set cardUseAmount=?,totalScore=?,lastJoinRound=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserMusicRoomRankData userMusicRoomRankData) {
        userMusicRoomRankData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankData.getCardUseAmount()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomRankData.getTotalScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankData.getLastJoinRound()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomRankData.getUserId()));
        if (getDb().executeNoneQuery(updateUserMusicRoomRankDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserMusicRoomRankDataSql = "insert into `t_u_activity_music_room_rank`(activityId,userId,cardUseAmount,totalScore,lastJoinRound) values (?,?,?,?,?)";
    @Override
    protected boolean addData(UserMusicRoomRankData userMusicRoomRankData) {
        userMusicRoomRankData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomRankData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankData.getCardUseAmount()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomRankData.getTotalScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankData.getLastJoinRound()));
        if (getDb().executeNoneQuery(insertUserMusicRoomRankDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserMusicRoomRankData parseData(ResultSet rs) throws SQLException {
        UserMusicRoomRankData userMusicRoomRankData = new UserMusicRoomRankData();
        userMusicRoomRankData.setActivityId(rs.getInt("activityId"));
        userMusicRoomRankData.setUserId(rs.getLong("userId"));
        userMusicRoomRankData.setCardUseAmount(rs.getInt("cardUseAmount"));
        userMusicRoomRankData.setTotalScore(rs.getLong("totalScore"));
        userMusicRoomRankData.setLastJoinRound(rs.getInt("lastJoinRound"));

        return userMusicRoomRankData;
    }

}
